package com.guigu.www.denghao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.denghao.service.DHRubricService;
import com.guigu.www.mapper.ProposeMapper;
import com.guigu.www.mapper.RubricMapper;
import com.guigu.www.mapper.RubricOptionMapper;
import com.guigu.www.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class DHRubricServiceImpl implements DHRubricService {

    @Autowired
    private RubricMapper rubricMapper;
    @Autowired
    private RubricOptionMapper optionMapper;
    @Autowired
    private ProposeMapper proposeMapper;

    // 查询分页的方法
    @Override
    public PageVo<Rubric> initRub(Integer pageNum, Integer pageSize, String rubName, String rubType) {
        IPage<Rubric> iPage = new Page<>(pageNum,pageSize);
        QueryWrapper<Rubric> wrapper = new QueryWrapper<>();
        wrapper.like("rub_name",rubName);
        wrapper.eq("rub_state",0);
        if (!rubType.equals("")){
            wrapper.eq("rub_type",rubType);
        }
        iPage = rubricMapper.selectPage(iPage,wrapper);
        return new PageVo<>(iPage.getTotal(),iPage.getRecords());
    }

    // 添加题目的方法
    @Override
    public Integer addRub(Rubric rubric) {
        // 获取当前时间
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format( now );
        rubric.setRubCreateTime(time);
        rubric.setRubState(0);// 设置初始状态
        Integer insert = rubricMapper.insert(rubric);
        for (RubricOption option : rubric.getOptions()) {
            option.setRubId(rubric.getRubId());
            optionMapper.insert(option);
        }
        Propose propose = rubric.getPropose();
        propose.setRubId(rubric.getRubId());
        propose.setPropoTitle(rubric.getRubName());
        proposeMapper.insert(propose);
        return insert;
    }

    @Override
    public List<RubricOption> watchRub(Integer rubId) {
        return optionMapper.selectList(new QueryWrapper<RubricOption>().eq("rub_id",rubId));
    }

    @Override
    public Integer delRub(Integer rubId, Integer rubState) {
        Rubric rubric = new Rubric();
        rubric.setRubId(rubId);
        rubric.setRubState(rubState);
        return rubricMapper.updateById(rubric);
    }

    @Override
    public Rubric queryByIdRubric(Integer rubId) {
        Rubric rubric = rubricMapper.selectById(rubId);
        List<RubricOption> rubricOptions = optionMapper.selectList(new QueryWrapper<RubricOption>().eq("rub_id", rubId));
        Propose propose = proposeMapper.selectOne(new QueryWrapper<Propose>().eq("rub_id", rubId));
        rubric.setOptions(rubricOptions);
        rubric.setPropose(propose);
        return rubric;
    }

    @Override
    public Integer upRub(Rubric rubric) {
        Integer i = rubricMapper.updateById(rubric);
        optionMapper.delete(new QueryWrapper<RubricOption>().eq("rub_id",rubric.getRubId()));
        for (RubricOption option : rubric.getOptions()) {
            option.setRubId(rubric.getRubId());
            optionMapper.insert(option);
        }
        proposeMapper.delete(new QueryWrapper<Propose>().eq("rub_id",rubric.getRubId()));
        Propose propose = rubric.getPropose();
        propose.setRubId(rubric.getRubId());
        propose.setPropoTitle(rubric.getRubName());
        proposeMapper.insert(propose);
        return i;
    }

}
